// pages/mall/mallList/mallList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req: false,
    showMask: false,
    showUrl: '',
    list: [],
    showDetail: {},
    shopRecord:{},
    params: {
      pageNo: 1,
      pageSize: 20
    }
  },
  changeValue(e) {
    this.toggle(e.currentTarget.dataset.param, e.detail.value);
    console.log(this.data.showDetail)
    if (this.data.showDetail.totalNumber * this.data.showDetail.usePoints > this.data.showDetail.points.points) {
      wx.showToast({
        title: '积分不足',
        icon: 'none',
        duration: 1500,
        success: (res) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    }
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  imageLoad: function (e) {
    var $width = e.detail.width, //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height; //图片的真实宽高比例
    var viewWidth = 360, //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 360 / ratio; //计算的高度值
    var image = this.data.images;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    var index = e.target.dataset.index;
    image[index] = {
      width: viewWidth,
      height: viewHeight
    }

    this.setData({
      images: image
    })
    console.log(this.data.images)
  },
  toggleDialog() {
    this.toggle("showMask", false);
  },
  baseInfo(callback) {
    app.req({
      url: "/common/static/base",
      success(res) {
        if (200 == res.code) {
          callback && callback(res.result.points || 0)
        }
      }
    })
  },
  openMallDetail(e) {
    let that = this;
    this.baseInfo(function (point) {
      let data = e.currentTarget.dataset.item;
      data.points = point;
      data.totalNumber = 0;
      that.toggle("showDetail", data);
      that.toggle("showMask", true);
    });

  },
  list(append) {
    let that = this;
    that.toggle("req", false);
    app.req({
      url: "/shop/vipShop/listForMini",
      data: that.data.params,
      complete() {
        that.toggle("req", true);
      },
      success(res) {
        if (200 == res.code) {
        

          let rlist =res.result.records;
          for (let i of rlist) {
            i.picUrl = i.picList.split(",")[0];
          }

          let list = that.data.list;
          // for (let i of list) {
          //   i.picUrl = i.picList.split(",")[0];
          // }
          that.toggle("list", append ? list.concat(rlist) : rlist);
          console.log(list)
          that.toggle("params.totalPages", res.result.pages)
        }
      }
    })
  },

 notify(e){
   let that = this;
   var shopRecord={};
   shopRecord.commodityId=that.data.showDetail.id;
   shopRecord.comNum=that.data.showDetail.totalNumber;
   if (shopRecord.comNum < 1){
     app.showToast({
      title: '请输入数量',
      icon: 'none',
      duration: '2000'
    });
     return;
   }
   app.req({
    url: "/shoprecord/shopRecord/add",
    method: 'post',
    data: shopRecord,
    success(res) {
      if (200 == res.code) {
        app.showToast({
          title: '下单成功',
          icon: 'success'
        })
      } else {
        app.showToast({
          title: '下单失败，请重试',
          icon: 'none'
        })
      }
    }
  });
 },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toggle("showUrl", app.globalData.showUrl);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  refreshData() {
    let that = this;
    app.getUserInfo({
      success() {
        that.toggle("params.pageNo", 1);
        that.list();
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.refreshData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let params = this.data.params;
    if (params.totalPages > params.pageNo) {
      this.toggle("params.pageNo", params.pageNo + 1);
      this.list(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})